var mockupbuild = require("../mockup");
var path = require('path');
var basename = path.basename(__filename, ".js");
var Mock = require('mockjs');
var Chance = require('chance');
var Random = Mock.Random;
var chance = new Chance();

chance.mixin({
    'pengyouquanuser': function () {
        return this.pickone([Random.cname(), Random.name()]);
    },
    'pengyouquansentence': function() {
        return this.pickone([Random.csentence(), Random.sentence()]);
    },
    'pengyouquancommoent': function() {
        let self = this;
        return self.pengyouquansentence();
    }
});

Function.prototype.auiClone = function() {
    var that = this;
    var temp = function temporary() { return that.apply(this, arguments); };
    for(var key in this) {
        if (this.hasOwnProperty(key)) {
            temp[key] = this[key];
        }
    }
    return temp;
};

function chooseone(obj, searched, findarr) {
    var isContainInSearched = findarr.every(function(v) {
        return searched.includes(v);
    });
    if (isContainInSearched) {
        let servied = chance.pickone(findarr);
        findarr.forEach(function(a) {
            if (a !== servied) {
                delete obj[a];
            }
        });
    }
    return isContainInSearched;
}

Mock.Handler._object = Mock.Handler.object.auiClone();
Mock.Handler.object = function(options) {
    let ret = null;
    if (options.name.indexOf("body") > -1) {
        ret = Mock.Handler._object(options);
        let keys = Object.keys(ret);

        // 处理text link
        if (keys.indexOf("text") > -1 || keys.indexOf("link") > -1) {
            if (keys.indexOf("text") > -1 && keys.indexOf("link") > -1) {
                let deleted = chance.pickone(["text", "link"]);
                delete ret[deleted];
            }
        } else {
            ret["text"] = chance.pengyouquansentence();
        }

        chooseone(ret, keys, ["gallery", "videos"]);
    } else {
        ret = Mock.Handler._object(options);
    }
    return ret;
};

let tplbody = function () {
    return chance.pickone([
        {
            'money': (function () {
                return '¥' + chance.floating({min: 0.1, max: 19020, fixed: 2});
            })(),
            "name": (function () {
                return "转账给" + Random.cname()
            })(),
            "content":"",
            "type":"normel",
        },
        {
            'money': (function () {
                return '¥' + chance.floating({min: 0.1, max: 19020, fixed: 2});
            })(),
            "name": (function () {
                return "已收钱";
            })(),
            "content":"",
            "type":"get",
        }
    ]);
}

let mutbody = function () {
    return chance.pickone([
        {
            'money': '活动账单',
            "name": "还有一人未支付",
            "content":"共收款199元，已收款150元",
            "type":"normel"
        },
        {
            'money': '活动账单',
            "name": "支付完成",
            "content":"共收款199元，已收款199元",
            "type":"get"
        },
        {
            'money': '活动账单',
            "name": "待支付",
            "content":"",
            "type":"normel2"
        },
    ])
}

// 'single': function() {
//     return chance.pickone([
//         {
//             'money': (function () {
//                 return '¥' + chance.floating({min: 0.1, max: 19020, fixed: 2});
//             })(),
//             "name": "转账给吴慧",
//             "content":"",
//             "type":"normel"
//         },
//         {
//             'money': (function () {
//                 return '¥' + chance.floating({min: 0.1, max: 19020, fixed: 2});
//             })(),
//             "name": "已收钱",
//             "content":"",
//             "type":"get"
//         }
//     ])
// },
// 'multi': function() {
//     return chance.pickone([
//         {
//             'money': '活动账单',
//             "name": "还有一人未支付",
//             "content":"共收款199元，已收款150元",
//             "type":"normel"
//         },
//         {
//             'money': '活动账单',
//             "name": "支付完成",
//             "content":"共收款199元，已收款199元",
//             "type":"get"
//         },
//         {
//             'money': '活动账单',
//             "name": "待支付",
//             "content":"",
//             "type":"normel2"
//         },
//     ])
// },

let template = {
    // 属性 list 的值是一个数组，其中含有 1 到 10 个元素
    'single|10': [Object.assign( tplbody, {
        // 属性 id 是一个自增数，起始值为 1，每次增 1
        'id|+1': 1,
        'create_time': function() {
            return Random.time('yyyy-MM-dd HH:mm:ss');
        }
    })],
    'multi|10': [Object.assign(mutbody, {
        // 属性 id 是一个自增数，起始值为 1，每次增 1
        'id|+1': 1,
        'create_time': function() {
            return Random.time('yyyy-MM-dd HH:mm:ss');
        }
    })]
};

mockupbuild(template, `./data/${basename}.json`);